<?php
namespace Service\OSS;

use Service\OSS;

class LocalOSS extends OSS {

    protected $config;

    public function __construct($config) {
        $this->config = $config;
    }

    public function upload($file) {
        $rs = array('path' => '', 'url' => '');

        $tmpName = $file['tmp_name'];

        $name = md5($file['name'] . time());
        $ext = strrchr($file['name'], '.');
        $uploadFolder = $this->config['upload_path'];
        if (!is_dir($uploadFolder)) {
            mkdir($uploadFolder, 0777);
        }

        $imgPath = $uploadFolder .  $name . $ext;
        if (move_uploaded_file($tmpName, $imgPath)) {
            $rs['path'] = $imgPath;
            $rs['url'] = sprintf('%s%s%s%s', $this->config['host'], $this->config['public_path'], $name, $ext);
        }

        return $rs;
    }
}
